<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "auth_item_child".
 *
 * @property string $parent
 * @property string $child
 *
 * @property AuthItem $parent0
 * @property AuthItem $child0
 */
class AuthItemChild extends Base
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'auth_item_child';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['parent', 'child'], 'string', 'max' => 64],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'parent' => 'Parent',
            'child' => 'Child',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getParent0()
    {
        return $this->hasOne(AuthItem::className(), ['name' => 'parent']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getChild0()
    {
        return $this->hasOne(AuthItem::className(), ['name' => 'child']);
    }

    public function getAuthItem(){
        return $this->hasOne(AuthItem::class, ['name' => 'child']);
    }

    public static function getFirstPermission($uid)
    {
        $query = self::find()
        ->leftJoin("auth_assignment", "auth_assignment.item_name = auth_item_child.parent")
        ->leftJoin("auth_item", "auth_item.name = auth_item_child.child")
        ->where(['auth_assignment.user_id' => $uid]);
        $data = $query->orderBy("sort asc")->one();
        return $data;
    }
}
